package com.tarena.luban.demo.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.function.Supplier;

//@Component
public class MyFilter01 implements GlobalFilter, Ordered {
    // 过滤器核心方法
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 拿到请求对象
        ServerHttpRequest request = exchange.getRequest();
        // 获取相关请求的所有数据
        // 1. 请求路径 第一种uri凭借的路径 第二种path
        URI uri = request.getURI();
        RequestPath path = request.getPath();
        System.out.println("当前请求uri"+uri);
        System.out.println("当前请求路径path"+path);
        // 2.请求方法 GET POST PUT DELETE OPTION
        HttpMethod method = request.getMethod();
        System.out.println("当亲请求的方法method"+method);
        // 3.请求参数
        MultiValueMap<String, String> queryParams = request.getQueryParams();
        List<String> name = queryParams.get("name");
        System.out.println("name"+name);
        // 请求头获取
        HttpHeaders headers = request.getHeaders();
        List<String> strings = headers.get("user-agent");
        System.out.println("请求头数据为："+strings);
        //5.其它 都是通过get方法获取
        InetSocketAddress remoteAddress = request.getRemoteAddress();//客户端ip地址
        System.out.println("当前请求客户端ip:"+remoteAddress.getAddress());
        System.out.println("当前请求客户端ip:"+remoteAddress.getHostName());

        // 控制响应对象response
        // 1. 拿到响应
        ServerHttpResponse response = exchange.getResponse();
        // 2. 可以获取当前响应数据 比如头
        HttpHeaders headersResponse = response.getHeaders();
        // 3. 写出有中文 注意乱码的问题 application/json-charset-utf-8
        headersResponse.setContentType(MediaType.APPLICATION_JSON);
        // 4. 准备一个输出字符串
        String resultJson = "{\"code\":500,\"message\":\"你的请求被拦截了\"}";
        // 5. 将字符串包装到响应中 从这个过滤器输出响应
        // 5.1 从响应中拿到转化的一个数据缓存对象 用这个对象包装一些字符串 二进制
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        // 5.2 使用缓存数据流对象 将字符串 为禁止包装
        DataBuffer wrap = dataBufferFactory.wrap(resultJson.getBytes(StandardCharsets.UTF_8));
        // 5.3 创建一个mono对象，可以接收dataBuffer
        Mono<DataBuffer> writeData = Mono.fromSupplier(new Supplier<DataBuffer>() {
            @Override
            public DataBuffer get() {
                return wrap;
            }
        });
        // 5.4 使用response输出返回值
        return response.writeWith(writeData);
    }

    @Override
    public int getOrder() {
        return 100;
    }
}
